﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using A2DFramework.LogService.Configuration;

namespace A2DFramework.LogService.LogProviders
{
    public class TextFileLogger : ILog
    {
        public void Log(Exception ex, params string[] additionalInfos)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("Message: {0}", ex.Message));
            sb.AppendLine(string.Format("Source: {0}", ex.Source));
            sb.AppendLine(string.Format("StackTrace: {0}", ex.StackTrace));
            sb.AppendLine(string.Format("TargetSite: {0}", ex.TargetSite));
            if (ex.InnerException != null)
            {
                sb.AppendLine(string.Format("Inner Exception Message: {0}", ex.InnerException.Message));
                sb.AppendLine(string.Format("Inner Exception Source: {0}", ex.InnerException.Source));
                sb.AppendLine(string.Format("Inner Exception StackTrace: {0}", ex.InnerException.StackTrace));
                sb.AppendLine(string.Format("Inner Exception TargetSite: {0}", ex.InnerException.TargetSite));
            }
            Log(sb.ToString(), additionalInfos);
        }

        public void Log(string message, params string[] additionalInfos)
        {
            if (!LogConfiguration.LogEnabled)
            {
                return;
            }

            string fileName = LogConfiguration.LogFileName;
            if (fileName == null || fileName == "")
            {
                fileName = HttpContext.Current.Server.MapPath("~/Log.txt");
            }
            if (System.IO.File.Exists(fileName) == true)
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi.Length > 1024 * 1024 * LogConfiguration.LogFileSize)
                {
                    System.IO.File.Delete(fileName);
                }
            }

            using (StreamWriter sw = System.IO.File.AppendText(fileName))
            {
                string msg = GetLogMessageFormat();
                msg = msg.Replace("{Now}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                msg = msg.Replace("{Message}", message);
                string additionalInfoMsg = "";
                if (additionalInfos != null && additionalInfos.Length > 0)
                    foreach(var info in additionalInfos)
                        additionalInfoMsg +=string.Format("{0}, ", info);

                additionalInfoMsg = additionalInfoMsg.TrimEnd(", ".ToCharArray());
                msg = msg.Replace("{AdditionalInfo}", additionalInfoMsg);
                sw.WriteLine(msg);
                sw.Flush();
                sw.Close();
            }
        }

        public string GetLogMessageFormat()
        {
            string msg = @"{Now}
---------------------------------------------
{Message}
Additional Information:
{AdditionalInfo}
---------------------------------------------\
";
            return msg;
        }
    }
}
